package org.thomias.exo6.monitor;

import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MenuDetectEvent;
import org.eclipse.swt.events.MenuDetectListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.thomias.loggertest.YT;

public class Application extends ApplicationWindow {
	Table table;
	AppMonitor m;
	private CancelAction cancelAction;

	public static void main(String args[]) {
		try {
			YT.init();
			Application window = new Application();
			window.m = new AppMonitor(window);
			window.m.start();
			window.setBlockOnOpen(true);
			window.open();
			Display.getCurrent().dispose();
			window.m.stop();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public Application() {
		super(null);
		addToolBar(SWT.FLAT | SWT.WRAP);
		createActions();
		addMenuBar();
		addStatusLine();
	}

	private void createActions() {
		cancelAction = new CancelAction(
				Application.this);
	}

	public void init() {
		for (int i = 0; i < 10; i++) {
			MonitorableTask task = new MonitorableTask("task" + i);
			Task task2 = new Task("task" + (100 + i));
			m.monitor(task);
			m.monitor(task2);
		}
	}

	@Override
	protected Control createContents(Composite parent) {

		Composite composite = new Composite(parent, SWT.NONE);
		GridLayout gl_composite = new GridLayout(1, false);
		gl_composite.marginHeight = 0;
		composite.setLayout(gl_composite);

		table = new Table(composite, SWT.BORDER | SWT.FULL_SELECTION);
		table.addMenuDetectListener(new MenuDetectListener() {
			public void menuDetected(MenuDetectEvent e) {
				final MenuManager myMenu = new MenuManager("xxx");
				final Menu menu = myMenu.createContextMenu(table.getShell());

				int sel = table.getSelection().length;
				if (sel > 0) {
					myMenu.add(cancelAction);
				}
				menu.setEnabled(true);
				myMenu.setVisible(true);
				menu.setVisible(true);
			}
		});
		table.setHeaderVisible(true);
		table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

		TableColumn tblclmnName = new TableColumn(table, SWT.NONE);
		tblclmnName.setWidth(57);
		tblclmnName.setText("Name");

		TableColumn tblclmnSize = new TableColumn(table, SWT.NONE);
		tblclmnSize.setWidth(33);
		tblclmnSize.setText("Size");

		TableColumn tblclmnUd = new TableColumn(table, SWT.NONE);
		tblclmnUd.setWidth(33);
		tblclmnUd.setText("U/D");

		TableColumn tblclmnProgress = new TableColumn(table, SWT.NONE);
		tblclmnProgress.setWidth(92);
		tblclmnProgress.setText("Progress");

		TableColumn tblclmnSpeed = new TableColumn(table, SWT.NONE);
		tblclmnSpeed.setWidth(50);
		tblclmnSpeed.setText("Speed");

		TableColumn tblclmnElapsed = new TableColumn(table, SWT.NONE);
		tblclmnElapsed.setWidth(58);
		tblclmnElapsed.setText("Elapsed");

		TableColumn tblclmnStarted = new TableColumn(table, SWT.NONE);
		tblclmnStarted.setWidth(58);
		tblclmnStarted.setText("Started");

		TableColumn tblclmnEstimated = new TableColumn(table, SWT.NONE);
		tblclmnEstimated.setWidth(65);
		tblclmnEstimated.setText("Estimated");

		init();
		return composite;
	}

	@Override
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText("Multithread bars");
	}
}
